﻿using System;
using Epitech.Krisk.Xna.UI.Game.InGame.Gameplay;
using Epitech.Krisk.Xna.UI.Game.Utils;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;
using Epitech.Krisk.Xna.ScreenManagerLib.Widget;
using Epitech.Krisk.Xna.UI.Game.InGame;

namespace Epitech.Krisk.Xna.UI.Game.Menu
{
    class GameOptionsView : MenuScreen
    {
        //Texture2D _bg;
        private RenderBackground _bg;
        private Slider _nbplayer;
        private MenuEntry _classicEntry;
        private MenuEntry _questEntry;
        private GamePlayType _gamePlay = GamePlayType.None;

        public GameOptionsView()
            : base("Nouvelle partie")
        {
            // Create our menu entries.

            LayoutEntry Menu = new LayoutEntry(new Vector2(1575f, 870f));
            Menu.Alignement = LayoutEntry.TextAlignement.Right;
            MenuEntry playGameEntry = new MenuEntry("[ Lancer la partie ]");
            MenuEntry mainMenuEntry = new MenuEntry("[ Menu principal ]");

            LayoutEntry Game = new LayoutEntry(new Vector2(830f, 175f));
            Game.Alignement = LayoutEntry.TextAlignement.Left;
            Game.LineSpacing = 190;
            _classicEntry = new MenuEntry("Game/GameClassic", true);
            _classicEntry.Selected += SelectedGame;
            _classicEntry.SelectedColor = Color.White;
            _questEntry = new MenuEntry("Game/GameQuest", true);
            _questEntry.Selected += SelectedGame;
            _questEntry.SelectedColor = Color.White;

            LayoutEntry sll = new LayoutEntry(new Vector2(925f, 605f));
            _nbplayer = new Slider(2, 5, 2)
                            {
                                FontSize = 1f,
                                SelectedColor = new Color(119, 121, 81),
                                DefaultColor = new Color(208, 44, 6),
                                IsPulse = false
                            };
            sll.AddEntry(_nbplayer);

            playGameEntry.Selected += StartGame;
            mainMenuEntry.Selected += DisplayMainMenu;

            Menu.AddEntry(playGameEntry);
            Menu.AddEntry(mainMenuEntry);

            Game.AddEntry(_classicEntry);
            Game.AddEntry(_questEntry);

            // Add entries to the menu.
            MenuEntries.Add(Game);
            MenuEntries.Add(sll);
            MenuEntries.Add(Menu);

            foreach (MenuEntry item in Menu.MenuEntries)
            {
                item.FontSize = 1f;
                item.SelectedColor = new Color(119, 121, 81);
                item.DefaultColor = new Color(208, 44, 6);
                item.IsPulse = false;
            }
        }

        private void SelectedGame(object sender, PlayerIndexEventArgs playerIndex)
        {
            MenuEntry game = sender as MenuEntry;

            if (game != null)
            {
                foreach (MenuEntry item in MenuEntries[0].MenuEntries)
                {
                    item.DefaultColor = Color.White;
                    item.SelectedColor = item.DefaultColor;
                }
                game.DefaultColor = Color.DarkSeaGreen;
                game.SelectedColor = game.DefaultColor;
                if (game.Text == "Game/GameClassic")
                    _gamePlay = GamePlayType.Classic;
                else if (game.Text == "Game/GameQuest")
                    _gamePlay = GamePlayType.Domination;
            }
        }

        public override void Draw(GameTime gameTime)
        {

            _bg.Draw(gameTime);
            this.PrintText("Type de jeu", 600f, 175f);
            this.PrintText("Nombre de joueurs", 520f, 640f);
            base.Draw(gameTime);
        }

        public override void LoadContent()
        {
            //_bg = ScreenManager.Game.Content.Load<Texture2D>("Game/Background");
            _bg = new RenderBackground("Game/Background");
            _bg.Content = ScreenManager.Game.Content;
            _bg.SpriteBatch = ScreenManager.SpriteBatch;
            _bg.ViewPort = ScreenManager.GraphicsDevice.Viewport;
            _bg.LoadContent();
            base.LoadContent();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            _bg.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public void PrintText(string text, float x, float y)
        {
            SpriteBatch sp = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;

            sp.Begin();

            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            // Draw the menu title centered on the screen
            Vector2 titlePosition = new Vector2(x, y);
            Vector2 titleOrigin = font.MeasureString(text) / 2;
            Color titleColor = new Color(255, 255, 255) * TransitionAlpha;
            float titleScale = 0.8f;

            titlePosition.Y -= transitionOffset * 100;

            sp.DrawString(font, text, titlePosition, titleColor, 0,
                                   titleOrigin, titleScale, SpriteEffects.None, 0);

            sp.End();
        }

        #region Handle Input


        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.Game.Exit();
        }

        protected void StartGame(object sender, PlayerIndexEventArgs playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");  
            if (_gamePlay != GamePlayType.None)
            {
                ScreenManager.RemoveScreen(this);
                ScreenManager.AddScreen(new InGameScreen(int.Parse(_nbplayer.Text), _gamePlay), null);
            }
            else
            {
                ScreenManager.AddScreen(new InfoMessage("Veuillez choisir\nun type de jeu"), null);
            }
        }

        protected void DisplayMainMenu(object sender, PlayerIndexEventArgs playerIndex)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.RemoveScreen(this);
            ScreenManager.AddScreen(new MainMenu(), null);
        }

        #endregion
    }
}
